<?php
namespace App\Models\Rating;

use App\Models\Educaton\MasterTeacher;
use App\Models\Model;
use App\Models\Teaching\TeachingSign;
use App\Models\Teaching\TeachingSubject;

class CheckTeacher extends Model
{
    protected $appends = [
        'grade_array',
        'subject_array',
    ];

    public function check_scores()
    {
        return $this->hasMany(CheckScore::class,'ct_id','id');
    }

    public function check_score()
    {
        return $this->hasOne(CheckScore::class,'ct_id','id');
    }

    public function checks()
    {
        return $this->belongsTo(Check::class,'check_id','id');
    }

    public function teaching_subject()
    {
        return $this->hasMany(TeachingSubject::class,'user_id','user_id');
    }

    public function master_teacher()
    {
        return $this->hasOne(MasterTeacher::class,'user_id','user_id');
    }

    public function getGradeArrayAttribute()
    {
        $grades = !empty($this->relations['teaching_subject']) ? $this->relations['teaching_subject'] : '';
     
        if(!$grades){
            return [];
        }
        $a = $grades->pluck('grade_set.grade_items')->flatten(1)->unique('selectValue')->sortBy('optionId');   
        return $a;
    }

    public function getSubjectArrayAttribute()
    {
        $subject = !empty($this->relations['teaching_subject']) ? $this->relations['teaching_subject'] : '';
        if(!$subject){
            return [];
        }
        $a = $subject->pluck('subject_set')->flatten(1);      
        return $a;
    }



}